package com.jcark.utilscode.util

import android.content.Context
import androidx.annotation.RawRes
import java.io.BufferedReader
import java.io.InputStreamReader

/**
 * Description:raw资源文件工具类 <br>
 *
 * @author King.Zi<br>
 * @date 2025/02/27<br>
 * Email: 78688689@qq.com
 */
object RawUtils {

    fun <T> rawToList(@RawRes id: Int, t: Class<T>, context: Context): List<T> {
        val json = readRaw(id, context)
        return GsonUtils.toList(json, t)
    }

    fun <T> rawToBean(@RawRes id: Int, t: Class<T>, context: Context): T {
        val json = readRaw(id, context)
        return GsonUtils.toBean(json, t)
    }

    fun readRaw(@RawRes id: Int, context: Context): String {
        val inputStream = context.resources.openRawResource(id)
        val reader = BufferedReader(InputStreamReader(inputStream))
        val stringBuilder = StringBuilder()
        var line: String?
        while (reader.readLine().also { line = it } != null) {
            stringBuilder.append(line)
        }
        return stringBuilder.toString()

    }
}